﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;


public partial class Main_Inventory_LedgerAllocation : System.Web.UI.Page
{
    Datautility du = new Datautility();
    string sql = "";
    protected void Page_Load(object sender, EventArgs e)
    {
       if (Session["userid"] == null)
        {
            //ScriptManager.RegisterStartupScript(Page, this.GetType(), "redirect", "window.open('../../login.aspx','_parent');", true);
            Response.Redirect("~/SessionExpired.aspx");
        }
          if (Request.QueryString["action"] != null)
          {
              if (Request.QueryString["action"].ToString() == "addnew")
              {
                  PnlAddNew.Visible = true;
                  if (!IsPostBack)
                  {
                      BindGridLocalSales();
                      BindGridInter();
                      BindGrdItem();
                  }
              }
              else if (Request.QueryString["action"].ToString() == "view")
              {
                  if (!IsPostBack)
                  {
                      PnlView.Visible = true;                     
                  }
              }
          }
    }

    protected void BindGridInter()
    {
        SqlParameter[] parm = { new SqlParameter("@mainclassid", "6") };
        DataTable dt = du.getdatausingprocedure("Usp_IVR_GetLedgerAllocation", parm);
        GrdInter.DataSource = dt;
        GrdInter.DataBind();
    }
    protected void BindGridLocalSales()
    {
        SqlParameter[] parm = { new SqlParameter("@mainclassid", "3") };
        DataTable dt = du.getdatausingprocedure("Usp_IVR_GetLedgerAllocation", parm);
        GrdLocal.DataSource = dt;
        GrdLocal.DataBind();
    }
    protected void BindGrdItem()
    {
        DataTable dt = du.getdatausingprocedure("Usp_IVR_GetLedgerAllocationByPurchase");
        GrdItem.DataSource = dt;
        GrdItem.DataBind();
    }
    protected void GrdItem_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            HiddenField HF = (HiddenField)e.Row.FindControl("HidPurLed");
            DropDownList ddledger = (DropDownList)e.Row.FindControl("DDlItemledger");
            sql = "select LID, PGID, LNAME from Acc_ledmast where pgid = '25'";
            DataTable dt = du.getdata(sql);
            ddledger.DataSource = dt;
            ddledger.DataValueField = "LID";
            ddledger.DataTextField = "Lname";
            ddledger.DataBind();
            ListItem list1 = new ListItem("", "0");
            ddledger.Items.Add(list1);
            if (HF.Value != string.Empty)
            {
                ddledger.SelectedValue = HF.Value;
            }
            else
            {
                list1.Selected = true;               
            }
        }
    }
    protected void GrdLocal_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            //e.Row.Cells[0].RowSpan = 8;
            HiddenField HF = (HiddenField)e.Row.FindControl("HidPurcLed");
            DropDownList ddllocal = (DropDownList)e.Row.FindControl("DDlLocalledger");
            sql = "select LID, PGID, LNAME from Acc_ledmast where pgid = '57'";
            DataTable dt = du.getdata(sql);
            ddllocal.DataValueField = "LID";
            ddllocal.DataTextField = "LNAME";
            ddllocal.DataSource = dt;
            ddllocal.DataBind();
            if (e.Row.RowIndex == 0)
            {
                e.Row.Cells[0].RowSpan = ((DataTable)GrdLocal.DataSource).Rows.Count;
            }
            else
            {
                e.Row.Cells.Remove(e.Row.Cells[0]);
            }
            if (e.Row.Cells[3].Text.Contains("6/6/2079"))
            {
                e.Row.Cells[3].Text = "Till Date";
            }
            ListItem list1 = new ListItem("", "0");
            ddllocal.Items.Add(list1);
            if (HF.Value != string.Empty)
            {
                ddllocal.SelectedValue = HF.Value;
            }
            else
            {
                list1.Selected = true;
            }
        }
    }
    protected void GrdInter_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            HiddenField HF = (HiddenField)e.Row.FindControl("HidPurLed");
            DropDownList ddlInter = (DropDownList)e.Row.FindControl("DDlInterledger");
            sql = "select LID, PGID, LNAME from Acc_ledmast where pgid = '57'";
            DataTable dt = du.getdata(sql);
            ddlInter.DataValueField = "LID";
            ddlInter.DataTextField = "LNAME";
            ddlInter.DataSource = dt;
            ddlInter.DataBind();
            if (e.Row.RowIndex == 0)
            {
                e.Row.Cells[0].RowSpan = ((DataTable)GrdInter.DataSource).Rows.Count;
            }
            else
            {
               e.Row.Cells.Remove(e.Row.Cells[0]);
            }
            if (e.Row.Cells[3].Text.Contains("06/Jun/2079"))
            {
                e.Row.Cells[3].Text = "Till Date";
            }
            ListItem list1 = new ListItem("", "0");
            ddlInter.Items.Add(list1);
            if (HF.Value != string.Empty)
            {
                ddlInter.SelectedValue = HF.Value;
            }
            else
            {
                list1.Selected = true;               
            }
        }
    }
    protected void UpdateGrdInter()
    {
        for (int i = 0; i < GrdInter.Rows.Count; i++)
        {
            GridViewRow gr = GrdInter.Rows[i];
            if (gr.RowType == DataControlRowType.DataRow)
            {
                DropDownList DdInter = (DropDownList)gr.FindControl("DDlInterledger");
                HiddenField hidinter = (HiddenField)gr.FindControl("HidTxdetid");
               
                    SqlParameter[] parm = { 
                                          new SqlParameter("@TXDETID",hidinter.Value ),
                                          new SqlParameter("@Purcsale_lid", DdInter.SelectedValue.ToString())
                                      };
                    du.executeprocedure("usp_ivr_updateTADETAILledger", parm);
                    lblMsg.Text = "updated";
            }
        }
    }
    protected void UpdateGrdLocal()
    {
        for (int i = 0; i < GrdLocal.Rows.Count; i++)
        {
            GridViewRow gr = GrdLocal.Rows[i];
            if (gr.RowType == DataControlRowType.DataRow)
            {
                DropDownList DdLocal = (DropDownList)gr.FindControl("DDlLocalledger");
                HiddenField hidLocal = (HiddenField)gr.FindControl("HidTxdetid");
                    SqlParameter[] parm = { 
                                          new SqlParameter("@TXDETID",hidLocal.Value ),
                                          new SqlParameter("@Purcsale_lid", DdLocal.SelectedValue.ToString())
                                      };
                    du.executeprocedure("usp_ivr_updateTADETAILledger", parm);
                    lblMsg.Text = "updated";
            }
        }
    }
    protected void UpdateGrdItem()
    {
        for (int i = 0; i < GrdItem.Rows.Count; i++)
        {
            GridViewRow gr = GrdItem.Rows[i];
            if (gr.RowType == DataControlRowType.DataRow)
            {
                DropDownList dItem = (DropDownList)gr.FindControl("DDlItemledger");
                HiddenField hitmid = (HiddenField)gr.FindControl("HidItmid");
                SqlParameter[] parm = { 
                                          new SqlParameter("@ITMID",hitmid.Value ),
                                          new SqlParameter("@Purcledger_lid", dItem.SelectedValue.ToString())
                                      };
                du.executeprocedure("USP_IVR_SaveItemOtherDetails", parm);
                lblMsg.Text = "updated";
            }
        }
    }
    protected void BtnSubmit_Click(object sender, EventArgs e)
    {
        UpdateGrdLocal();
        UpdateGrdInter();
        UpdateGrdItem();
    }
    protected void BtnCancel_Click(object sender, EventArgs e)
    {
    }
    protected void GrdInter_RowCreated(object sender, GridViewRowEventArgs e)
    {
    }
}
